package com.hst.cz.cameralib;

import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.Log;

public class WlCamera {

    private Camera camera;

    public WlCamera(){}

    private SurfaceTexture surfaceTexture;

    public Camera initCamera(SurfaceTexture surfaceTexture, int cameraId) throws Exception {
        this.surfaceTexture = surfaceTexture;

        camera = Camera.open(cameraId);
        camera.setPreviewTexture(surfaceTexture);
        //绑定SurfaceTexture和摄像头预览
        Camera.Parameters parameters = camera.getParameters();

        parameters.setFlashMode("off");
        parameters.setPreviewFormat(ImageFormat.NV21);

        camera.setParameters(parameters);
        camera.startPreview();

        return camera;
    }

    public void stopPreview()
    {
        if(camera != null)
        {
            try {
                camera.stopPreview();
            }catch (Exception e){
                e.printStackTrace();
            }

            try {
                camera.release();
            }catch (Exception e){
                e.printStackTrace();
            }

            camera = null;
        }
    }

//    public void changeCamera(int cameraId)
//    {
//        if(camera != null)
//        {
//            stopPreview();
//        }
//        initCamera(surfaceTexture,cameraId);
//    }
}